const defaultState = {
    value: '',
    list: [],
};

const reducer = (state = defaultState, action) => {
    console.log(state, action);
    let newState = JSON.parse(JSON.stringify(state)); // 深拷贝,不能直接修改state里的数据
    if (action.type === 'init_list') {
        newState.list = action.list;
    }
    if (action.type === 'change_input') {
        newState.value = action.value;
    }
    if (action.type === 'add_item') {
        newState.list.push(newState.value);
        newState.value = '';
    }
    if (action.type === 'del_item') {
        newState.list.splice(action.index, 1);
    }
    return newState;
};

export default reducer;